		TITLE	PE_DEBUG - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS

if	fg_pe
		INCLUDE	EXES
		INCLUDE	PE_STRUC


		PUBLIC	PE_INIT_DEBUG,PE_FINISH_DEBUG


		.DATA

		EXTERNDEF	TEMP_RECORD:BYTE

		EXTERNDEF	PE_DEBUG_OBJECT_GINDEX:DWORD,SEG_PAGE_SIZE_M1:DWORD,NOT_SEG_PAGE_SIZE_M1:DWORD
		EXTERNDEF	FINAL_HIGH_WATER:DWORD,PE_NEXT_OBJECT_RVA:DWORD,CV_HEADER_LOC:DWORD

		EXTERNDEF	PEXEHEADER:PEXE,PE_OBJECT_GARRAY:STD_PTR_S


		.CODE	PASS2_TEXT

		EXTERNDEF	CHANGE_PE_OBJECT:PROC,DO_OBJECT_ALIGN:PROC,MOVE_EAX_TO_EDX_FINAL:PROC


PE_INIT_DEBUG	PROC
		;
		;OUTPUT HEADER DESCRIBING DEBUG TYPE AND LOCATION
		;
;		BITT	CODEVIEW_FLAG
;		JZ	L9$

		CALL	CHANGE_PE_OBJECT
		ASSUME	EAX:PTR PE_OBJECT_STRUCT

		MOV	[EAX]._PEOBJECT_FLAGS,MASK PEL_INIT_DATA_OBJECT + MASK PEH_READABLE + MASK PEH_WRITABLE + MASK PEH_DISCARDABLE

		MOV	DPTR [EAX]._PEOBJECT_NAME,'bed.'

		MOV	DPTR [EAX]._PEOBJECT_NAME+4,'gu'

		MOV	ECX,PE_NEXT_OBJECT_RVA

		MOV	[EAX]._PEOBJECT_RVA,ECX
		MOV	PEXEHEADER._PEXE_DEBUG_RVA,ECX

		MOV	PEXEHEADER._PEXE_DEBUG_SIZE,SIZE PE_DEBUG_HDR_STRUCT

		MOV	EAX,FINAL_HIGH_WATER

		MOV	DEBUG_HEADER_ADDR,EAX
		ADD	EAX,SIZE PE_DEBUG_HDR_STRUCT

		MOV	FINAL_HIGH_WATER,EAX
		MOV	CV_HEADER_LOC,EAX
L9$:
		RET

PE_INIT_DEBUG	ENDP


PE_FINISH_DEBUG	PROC
		;
		;NEED TO WRITE OUT DEBUG HEADER
		;
;		BITT	CODEVIEW_FLAG
;		JZ	L9$

		PUSHM	EDI,ESI

		MOV	EDI,OFF TEMP_RECORD
		XOR	EAX,EAX

		MOV	ECX,PE_DEBUG_HDR_STRUCT._PDH_DEBUG_TYPE/4

		REP	STOSD

		MOV	AL,2			;CODEVIEW INFO

		STOSD

		MOV	EAX,FINAL_HIGH_WATER	;NEXT COMES SIZE OF DEBUG DATA, WITHOUT THIS HEADER
		MOV	ECX,CV_HEADER_LOC

		SUB	EAX,ECX

		STOSD

		OR	EAX,EAX
		JZ	L5$

		MOV	EAX,PEXEHEADER._PEXE_DEBUG_RVA	;NEXT COMES RVA OF RAW CODEVIEW DATA

		ADD	EAX,SIZE PE_DEBUG_HDR_STRUCT

		STOSD

		MOV	EAX,ECX			;FILE ADDRESS OF DEBUG DATA

		STOSD

		XOR	EAX,EAX

		STOSD

		JMP	L6$

L9$:
		RET

L5$:
		XOR	EAX,EAX
		MOV	ECX,3

		REP	STOSD
L6$:
		MOV	EAX,OFF TEMP_RECORD
		MOV	EDX,DEBUG_HEADER_ADDR

		MOV	ECX,SIZE PE_DEBUG_HDR_STRUCT
		CALL	MOVE_EAX_TO_EDX_FINAL
		;
		;MAKE SURE .debug OBJECT IS UP TO DATE, ALONG WITH NEXT_OBJECT_RVA
		;
		MOV	ESI,PE_DEBUG_OBJECT_GINDEX
		CONVERT	ESI,ESI,PE_OBJECT_GARRAY
		ASSUME	ESI:PTR PE_OBJECT_STRUCT

		MOV	EAX,TEMP_RECORD.PE_DEBUG_HDR_STRUCT._PDH_DEBUG_SIZE

		ADD	EAX,SIZE PE_DEBUG_HDR_STRUCT

		MOV	[ESI]._PEOBJECT_PSIZE,EAX

		DO_FILE_ALIGN_EAX

		MOV	[ESI]._PEOBJECT_VSIZE,EAX
		CALL	DO_OBJECT_ALIGN

		ADD	EAX,PEXEHEADER._PEXE_DEBUG_RVA
		POP	ESI

		MOV	PE_NEXT_OBJECT_RVA,EAX
		POP	EDI

		RET

PE_FINISH_DEBUG	ENDP


		.DATA?

DEBUG_HEADER_ADDR	DD	?

endif

		END

